package com.hiray.calendar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by hiray on 2018/2/27.
 *
 * @author hiray
 */

public class WeekView extends View {

    private int mTextColor;
    private Paint mPaint;
    private static final String[] WEEK_DAYS = {"日", "一", "二", "三", "四", "五", "六"};

    public WeekView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WeekView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.WeekView);
        mTextColor = array.getColor(R.styleable.WeekView_text_color,
                Color.parseColor("#56a6f6"));
        array.recycle();
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(mTextColor);
        mPaint.setTextSize(28);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int slice, h = getMeasuredHeight();
        float baseline, textLen, startX;
        slice = getMeasuredWidth() / 7;
        Paint.FontMetrics fm = mPaint.getFontMetrics();
        baseline = (fm.bottom - fm.top) / 2 - fm.bottom + h / 2;
        for (int i = 0; i < 7; i++) {
            textLen = mPaint.measureText(WEEK_DAYS[i]);
            startX = slice * i + (slice - textLen) / 2;
            canvas.drawText(WEEK_DAYS[i], startX, baseline, mPaint);
        }

    }
}
